package com.dyzx.hbb.modules.wms.controller;

import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseValidController;
import com.dyzx.hbb.modules.wms.entity.WmsStockInDetails;
import com.dyzx.hbb.modules.wms.entity.WmsStockInDetails;
import com.dyzx.hbb.modules.wms.query.WmsStockInDetailsQuery;
import com.dyzx.hbb.modules.wms.service.WmsStockInDetailsService;

import com.dyzx.hbb.modules.wms.vo.WmsStockInDetailsUVO;
import com.dyzx.hbb.modules.wms.vo.WmsStockInDetailsVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 入库单明细管理控制器
 * <p>处理仓库管理系统中入库单明细的查询和管理业务逻辑
 * <p>主要职责：
 * <ul>
 * <li>提供入库单明细的基础CRUD操作</li>
 * <li>支持按入库单ID和产品ID等条件筛选明细信息</li>
 * <li>维护入库单明细数据的排序展示</li>
 * </ul>
 * <p>设计意图：为仓库管理模块提供入库单明细的数据管理接口，
 * 确保入库明细数据的完整性和可追踪性
 * 
 * @author 开发团队
 * @version 1.0.0
 * @since 2023-05-30
 */
@Tag(name = "入库单明细管理", description = "入库单明细相关接口")
@RestController
@RequestMapping("/wms/stock/in/details")
public class WmsStockInDetailsController extends BaseValidController<
        WmsStockInDetailsService,
        WmsStockInDetails,
        WmsStockInDetailsQuery,
        WmsStockInDetailsVO,
        WmsStockInDetailsUVO
        > {

    @Autowired
    private WmsStockInDetailsService service;

    @Override
    protected WmsStockInDetailsService getBaseService() {
        return service;
    }

    @Override
    protected MyLambdaQueryWrapper<WmsStockInDetails> queryWrapper(WmsStockInDetailsQuery query) {
        MyLambdaQueryWrapper<WmsStockInDetails> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.orderByDesc(WmsStockInDetails::getCreateDate);
        queryWrapper.select(WmsStockInDetails::getId, WmsStockInDetails::getOrderId, WmsStockInDetails::getProductId, WmsStockInDetails::getLocationId, WmsStockInDetails::getLocationAreaId, WmsStockInDetails::getStatus, WmsStockInDetails::getHouseId,  WmsStockInDetails::getCreateDate, WmsStockInDetails::getUpdateDate);
        queryWrapper.houseName(WmsStockInDetails::getHouseId).as("house_name")
                .select(WmsStockInDetails::getQuantity);
        queryWrapper.wmsProductName(WmsStockInDetails::getProductId).as("product_name");
        queryWrapper.select(WmsStockInDetails::getUniPrice).as("price");
        queryWrapper.orderByDesc(WmsStockInDetails::getCreateDate);
        queryWrapper.organName(WmsStockInDetails::getOrganId).as("organ_name");
        queryWrapper.userName(WmsStockInDetails::getOperatorId).as("operator_name");

        if (query != null) {
            queryWrapper.eq(query.getOrderId() != null, WmsStockInDetails::getOrderId, query.getOrderId())
                    .eq(query.getProductId() != null, WmsStockInDetails::getProductId, query.getProductId());
        }
        return queryWrapper;
    }

    @Override
    protected MyLambdaQueryWrapper<WmsStockInDetails> queryInfoWrapper() {
        MyLambdaQueryWrapper<WmsStockInDetails> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.orderByDesc(WmsStockInDetails::getCreateDate);
        return queryWrapper;
    }

    @Override
    protected WmsStockInDetails addValid(WmsStockInDetailsVO entity) {
        return null;
    }

    @Override
    protected WmsStockInDetails editValid(WmsStockInDetailsUVO entity) {
        return null;
    }

    /*
     * @Description: 根据出库单ID查询出库单明细
     * @param: order_id
     *
     * */
    @GetMapping("/details")
    public Result<List<Map<String, Object>>> details(Long order_id) {
        MyLambdaQueryWrapper<WmsStockInDetails> queryWrapper =queryWrapper(null);
        queryWrapper.eq(WmsStockInDetails::getOrderId, order_id);
        List<Map<String, Object>> list = service.listMaps(queryWrapper);
        return list != null ? Result.ok(list) : Result.error(404, "暂无数据");
    }
}